# coding:utf-8
import sys
from typing import Union

from PySide6.QtCore import Qt, QSize, QRect
from PySide6.QtGui import QPainter, QColor, QIcon
from PySide6.QtWidgets import QWidget
from qfluentwidgets import isDarkTheme, NavigationInterface, NavigationItemPosition, FluentIconBase, \
    NavigationTreeWidget
from qfluentwidgets.common.animation import BackgroundAnimationWidget
from qfluentwidgets.components.widgets.frameless_window import FramelessWindow
from qframelesswindow import TitleBarBase

from common import StyleSheet
from .navigation import NavigationInterface
from .title_bar import CustomTitleBar
from ..widgets import SlidingStackedWidget


class FluentWindowBase(BackgroundAnimationWidget, FramelessWindow):
    """ Fluent window base class """

    def __init__(self, parent=None):
        self._isMicaEnabled = False
        self._lightBackgroundColor = QColor(240, 244, 249)
        self._darkBackgroundColor = QColor(32, 32, 32)
        super().__init__(parent)
        self.totalStackedWidget = SlidingStackedWidget(self)

        # show system title bar buttons on macOS
        if sys.platform == "darwin":
            self.setSystemTitleBarButtonVisible(False)

        self.totalStackedWidget.setGeometry(0, 0, self.width(), self.height())
        self.titleBar.raise_()

    def setCustomBackgroundColor(self, light, dark):
        """ set custom background color

        Parameters
        ----------
        light, dark: QColor | Qt.GlobalColor | str
            background color in light/dark theme mode
        """
        self._lightBackgroundColor = QColor(light)
        self._darkBackgroundColor = QColor(dark)
        self._updateBackgroundColor()

    def _normalBackgroundColor(self):
        return self._darkBackgroundColor if isDarkTheme() else self._lightBackgroundColor

    def paintEvent(self, e):
        super().paintEvent(e)
        painter = QPainter(self)
        painter.setPen(Qt.PenStyle.NoPen)
        painter.setBrush(self.backgroundColor)
        painter.drawRect(self.rect())

    def systemTitleBarRect(self, size: QSize) -> QRect:
        """ Returns the system title bar rect, only works for macOS

        Parameters
        ----------
        size: QSize
            original system title bar rect
        """
        return QRect(size.width() - 75, 0 if self.isFullScreen() else 9, 75, size.height())

    def setTitleBar(self, titleBar):
        super().setTitleBar(titleBar)

        # hide title bar buttons on macOS
        if sys.platform == "darwin" and self.isSystemButtonVisible() and isinstance(titleBar, TitleBarBase):
            titleBar.minBtn.hide()
            titleBar.maxBtn.hide()
            titleBar.closeBtn.hide()

    def resizeEvent(self, e):
        self.titleBar.move(180, 0)
        self.titleBar.resize(self.width() - 180, self.titleBar.height())
        self.totalStackedWidget.resize(self.size())

    def addTotalWidget(self, widget: QWidget):
        widget.setParent(self.totalStackedWidget)
        self.totalStackedWidget.addWidget(widget)

    def setTotalCurrentWidget(self, widget: QWidget):
        self.totalStackedWidget.setCurrentWidget(widget)

    def setTotalCurrentIndex(self, index: int):
        self.totalStackedWidget.setCurrentIndex(index)

    def removeTotalWidget(self, widget: QWidget):
        self.totalStackedWidget.removeWidget(widget)
        widget.deleteLater()


class FluentWindow(FluentWindowBase):
    """ Fluent window class """

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setTitleBar(CustomTitleBar(self))
        self.navigationWidget = NavigationInterface(self)

        self.addTotalWidget(self.navigationWidget)
        StyleSheet.Window.apply(self)

    @property
    def navigationInterface(self):
        return self.navigationWidget.navigationBar

    @property
    def stackedWidget(self):
        return self.navigationWidget.stackedWidget

    def addSubInterface(self, interface: QWidget, icon: Union[FluentIconBase, QIcon, str], text: str,
                        position=NavigationItemPosition.TOP) -> NavigationTreeWidget:
        return self.navigationWidget.addSubInterface(interface, icon, text, position)
